<?php

namespace App\Http\Controllers\Common\EnumData\Enumop\Video;

use App\Http\Controllers\Controller;
use App\Http\Requests\Common\EnumData\Enumop\Video\VyearRequest;
use App\Models\Common\EnumData\Enumop\Video\Vyear;
use App\Repositories\Common\EnumData\Enumop\Video\VyearRepository;
use Encore\Admin\Controllers\ModelForm;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Flash;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Route;
use Response;
use DB;

class VyearController extends Controller
{
    use ModelForm;

    /** @var  VyearRepository */
    private $vyearRepository;

    public function __construct(VyearRepository $vyearRepo)
    {
        $this->vyearRepository = $vyearRepo;
    }

    /**
     * Display a listing of the Vyear.
     *
     * @return Response
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header(__('common.enum_data.enumop.video.vyears.index_header'));
            $content->description(__('common.enum_data.enumop.video.vyears.index_description'));

            $content->body($this->grid());
        });
    }

    /**
     * Show the form for creating a new Vyear.
     *
     * @return Response
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header(__('common.enum_data.enumop.video.vyears.create_header'));
            $content->description(__('common.enum_data.enumop.video.vyears.create_description'));

            $content->body($this->form());

        });
    }

    /**
     * Store a newly created Vyear in storage.
     *
     * @param VyearRequest $request
     *
     * @return Response
     */
    public function store(VyearRequest $request)
    {
        $input = $request->all();

        $vyear = $this->vyearRepository->create($input);

        Flash::success(__('common.enum_data.enumop.video.vyears.saved_success'));

        return Input::get('_previous_') ? redirect(Input::get('_previous_')) : redirect(route('common.enum_data.enumop.video.vyears.index'));

    }

    /**
     * Display the specified Vyear.
     *
     * @param  int $id
     *
     * @return Response
     */
    public function show($id)
    {
        $vyear = $this->vyearRepository->findWithoutFail($id);

        if (empty($vyear)) {
            Flash::error(__('common.enum_data.enumop.video.vyears.not_found'));

            return redirect(route('common.enum_data.enumop.video.vyears.index'));
        }

        return view('common.enum_data.enumop.video.vyears.show')->with('vyear', $vyear);

    }

    /**
     * Show the form for editing the specified Vyear.
     *
     * @param  int $id
     *
     * @return Response
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header(__('common.enum_data.enumop.video.vyears.edit_header'));
            $content->description(__('common.enum_data.enumop.video.vyears.edit_description'));

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Update the specified Vyear in storage.
     *
     * @param  int              $id
     * @param VyearRequest $request
     *
     * @return Response
     */
    public function update($id, VyearRequest $request)
    {
        $vyear = $this->vyearRepository->findWithoutFail($id);

        if (empty($vyear)) {
            Flash::error(__('common.enum_data.enumop.video.vyears.not_found'));

            return redirect(route('common.enum_data.enumop.video.vyears.index'));
        }

        $vyear = $this->vyearRepository->update($request->all(), $id);
        


        Flash::success(__('common.enum_data.enumop.video.vyears.updated_success'));

        return Input::get('_previous_') ? redirect(Input::get('_previous_')) : redirect(route('common.enum_data.enumop.video.vyears.index'));
    }

    /**
     * Remove the specified Vyear from storage.
     *
     * @param  int $id
     *
     * @return Response
     */
    public function destroy($id)
    {
        // 根据 `,` 判断传递过来的是单个id还是多个id
        if (substr_count($id, ',') >= 1) {
            $id = explode(",", $id);
        }

        // 如果是数组则进行批量删除
        if (is_array($id)) {
            if ($flag = $this->vyearRepository->batchDelete('id', $id)) {
                return response()->json(['message' => __('common.enum_data.enumop.video.vyears.deleted_success'), 'status' => $flag]);
            } else {
                return response()->json(['message' => __('base.deleted.error'), 'status' => $flag]);
            }
        }

        $vyear = $this->vyearRepository->findWithoutFail($id);

        if (empty($vyear)) {
            Flash::error(__('common.enum_data.enumop.video.vyears.not_found'));

            return redirect(route('common.enum_data.enumop.video.vyears.index'));
        }

        if ($flag = $this->vyearRepository->delete($id)) {
        
            return response()->json(['message' => __('common.enum_data.enumop.video.vyears.deleted_success'), 'status' => $flag]);
        } else {
            return response()->json(['message' => __('base.deleted.error'), 'status' => $flag]);
        }
    }

    /**
     * [form description]
     * @return {[type]} [description]
     */
    protected function form()
    {
        return Admin::form(Vyear::class, function (Form $form) {

            $form->display('id', 'ID');
            $form->text('name', '年份名称')->help('必填');
            $form->text('title', '显示标题');
            $form->text('subtitle', '显示副标题');
            $form->text('short_desc', '描述');


            // $form->display('created_at', __('base.created_at'));
            // $form->display('updated_at', __('base.updated_at'));

        });
    }

    /**
     * [grid description]
     * @return {[type]} [description]
     */
    protected function grid()
    {

        return Admin::grid(Vyear::class, function (Grid $grid) {
            // 考虑是否需要scope和排序
            // $grid->model()->orderBy('listorder', 'asc');

            // //添加按钮
            // if (!\Gate::check('common.enum_data.enumop.video.vyears.create')) {
            //     $grid->disableCreation();
            // }

            // // 编辑和删除按钮
            // $grid->actions(function ($actions) {
            //     //编辑按钮
            //     if (!\Gate::check('common.enum_data.enumop.video.vyears.edit')) {
            //         $actions->disableEdit();
            //     }
            //     //删除按钮
            //     if (!\Gate::check('common.enum_data.enumop.video.vyears.destroy')) {
            //         $actions->disableDelete();
            //     }
            // });

            // //导出按钮
            // if (!\Gate::check('common.enum_data.enumop.video.vyears.export')) {
            //     $grid->disableExport();
            // }

            // //批量操作
            // $grid->tools(function ($tools) {
            //     $tools->batch(function ($batch) {
            //         //批量删除按钮
            //         if (!\Gate::check('common.enum_data.enumop.video.vyears.batch_destroy')) {
            //             $batch->disableDelete();
            //         }
            //     });
            // });

            //添加按钮
            if (Admin::user()->cannot('common.enum_data.enumop.video.vyears.create')) {
                $grid->disableCreation();
            }

            // 编辑和删除按钮
            $grid->actions(function ($actions) {
                //编辑按钮
                if (Admin::user()->cannot('common.enum_data.enumop.video.vyears.edit')) {
                    $actions->disableEdit();
                }
                //删除按钮
                if (Admin::user()->cannot('common.enum_data.enumop.video.vyears.destroy')) {
                    $actions->disableDelete();
                }
            });

            //导出按钮
            if (Admin::user()->cannot('common.enum_data.enumop.video.vyears.export')) {
                $grid->disableExport();
            }

            //批量操作
            $grid->tools(function ($tools) {
                $url =  route('entity.assets.assets.model',['sp_no' => '','config_file_select'=>'']);
                $tools->append('<a href=' .$url. ' class="btn btn-sm btn-warning pull-right" title="导入数据"><i class="fa fa-ban"></i>导入数据</a>');

                $tools->batch(function ($batch) {
                    //批量删除按钮
                    if (Admin::user()->cannot('common.enum_data.enumop.video.vyears.batch_destroy')) {
                        $batch->disableDelete();
                    }
                });
            });

            $grid->column('id', 'ID')->sortable();
            $grid->column('name', '年份名称')->sortable();
            $grid->column('title', '显示标题')->sortable();
            $grid->column('subtitle', '显示副标题')->sortable();
            $grid->column('short_desc', '描述')->sortable();

            
            /**
             * 过滤处理
             */
            $grid->filter(function ($filter) {
                // 禁用id查询框
                // $filter->disableIdFilter();
                // 名称
                $filter->like('name', '名称');
                // 标题
                $filter->like('title', '标题');
            });
        });
    }
}
